function TreeNode(val) {
    this.val = val
    this.left = null
    this.right = null
}

function buildTree(preorder, postorder) {
    function createTree(preorder, postorder, n) {
        if (n === 0) {
            return null
        }
        let node = new TreeNode(preorder[0])
        if (n === 1) {
            return node
        }
        let k = 0
        while (postorder[k] !== preorder[1]) {
            k += 1
        }
        node.left = createTree(preorder.slice(1, k + 2), postorder.slice(0, k + 1), k + 1)
        node.right = createTree(preorder.slice(k + 2, n), postorder.slice(k + 1, n - 1), n - k - 2)
        return node
    }
    return createTree(preorder, postorder, preorder.length)
}

let preorder = ['A', 'B', 'D', 'E', 'C', 'F']
let postorder = ['D', 'E', 'B', 'F', 'C', 'A']
console.log(buildTree(preorder, postorder))

